<?php

function clearTempFile() {
    $dirs = array('Application/Runtime/');
    foreach ($dirs as $v) {
        rmdirr($v);
    }
}

//处理方法
function rmdirr($dirname) {
    if (!file_exists($dirname)) {
        return false;
    }
    if (is_file($dirname) || is_link($dirname)) {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir) {
        while (false !== $entry = $dir->read()) {
            if ($entry == '.' || $entry == '..') {
                continue;
            }
            //递归
            rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
        }
    }
}

//公共函数
//获取文件修改时间
function getfiletime($file, $DataDir) {
    $a = filemtime($DataDir . $file);
    $time = date("Y-m-d H:i:s", $a);
    return $time;
}

//获取文件的大小
function getfilesize($file, $DataDir) {
    $perms = stat($DataDir . $file);
    $size = $perms['size'];
    // 单位自动转换函数
    $kb = 1024;         // Kilobyte
    $mb = 1024 * $kb;   // Megabyte
    $gb = 1024 * $mb;   // Gigabyte
    $tb = 1024 * $gb;   // Terabyte

    if ($size < $kb) {
        return $size . " B";
    } else if ($size < $mb) {
        return round($size / $kb, 2) . " KB";
    } else if ($size < $gb) {
        return round($size / $mb, 2) . " MB";
    } else if ($size < $tb) {
        return round($size / $gb, 2) . " GB";
    } else {
        return round($size / $tb, 2) . " TB";
    }
}

function getWuxianDaohang($id) {
    $detail = M("area")->where("id = " . $id . "")->find();
    if ($detail) {
        $str .= "<span style='margin:0 5px'>></span>" . "<a href='" . U("Area/lists", array("id" => $detail['id'])) . "'>" . $detail['name'] . "</a>";
        getWuxianDaohang($detail['pid']);
    }
    echo $str;
}

function getWuxianDaohang2($ids) {
    if ($ids == 0) {
        return "所有地区";
        exit;
    }
    $ids = explode(",", $ids);
    $Dao = M("area");
    if ($ids) {

        foreach ($ids as $v) {

            $detail = $Dao->where("id = " . $v . "")->find();
            $two = $Dao->where("id = " . $detail['pid'] . "")->find();
            $three = $Dao->where("id = " . $two['pid'] . "")->find();
            if ($detail) {
                $str .= "<p><a href='" . U("Area/lists", array("id" => $three['id'])) . "'>" . $three['name'] . "</a>
                    <span style='margin:0 5px'>></span><a href='" . U("Area/lists", array("id" => $two['id'])) . "'>" . $two['name'] . "</a>
                        <span style='margin:0 5px'>></span><a href='" . U("Area/lists", array("id" => $detail['id'])) . "'>" . $detail['name'] . "</a></p>";
            }
        }
        echo $str;
    }
}

function getAreaSubIds($id) {
    global $str;
    $lists = M("area")->field("id")->where("pid = " . $id . "")->select();
    if ($lists) {
        foreach ($lists as $v) {
            $str .= $v['id'] . ",";
            getAreaSubIds($v['id']);
        }
        return $str;
    }
}

function getAreaSelect($id, $rank = 0) {
    if ($id > 0) {
        $detail = M("area")->where("id = " . $id . "")->find();
        $areas = M("area")->where("pid = " . $detail['pid'] . "")->select();
        if ($areas) {//如果有子类 
            $rank--;
            $str = "<span class='g-input-selectbox'><select onchange=areaChange($(this)) autocomplete='off'><option value=''>请选择</option>";
            foreach ($areas as $v) {
                if ($v['id'] == $detail['id']) {
                    $select = "selected";
                } else {
                    $select = "";
                }

                $str .= "<option value= " . $v['id'] . " " . $select . ">" . $v['name'] . "</option>";
            }
            $str .= "</select></span>&nbsp;";

            if ($detail['pid'] > 0 && $rank > 0) {
                getAreaSelect($detail['pid'], $rank);
            }
        }
    } else {
        $areas = M("area")->where("pid = 0")->select();
        $str = "<span class='g-input-selectbox'><select  onchange=areaChange($(this)) autocomplete='off'><option value= ''>请选择</option>";
        foreach ($areas as $v) {
            $str .= "<option value= " . $v['id'] . ">" . $v['name'] . "</option>";
        }
        $str .= "</select></span>&nbsp;";
        for ($i = $rank - 1; $i > 0; $i--) {
            $str .= "<span class='g-input-selectbox'><select id ='area_" . $i . "' name ='area_" . $i . "' onchange=areaChange($(this)) autocomplete='off'>";

            $str .= "<option value= ''>请选择</option>";
            $str .= "</select></span>&nbsp;";
        }
    }
    echo $str;
}

function getWuxian($id = 0, $type = 0, $rank) {
    $Dao = M("area");
    if ($id > 0) {
        $detail = $Dao->where("id = " . $id . "")->find();
        $areas = $Dao->where("pid = " . $detail['pid'] . "")->select();
        if ($areas) {//如果有子类 
            $type++;

            $str = "<span class='g-input-selectbox'><select  onchange=areaChange($(this)) autocomplete='off'>";

            foreach ($areas as $v) {
                if ($v['id'] == $detail['id']) {
                    $select = "selected";
                } else {
                    $select = "";
                }

                $str .= "<option value= " . $v['id'] . " " . $select . ">" . $v['name'] . "</option>";
            }
            $str .= "</select></span>&nbsp;";
            if ($areas[0]['pid'] > 0) {
                getWuxian($areas[0]['pid'], $type);
            }
        }
    } else {
        $areas = $Dao->where("pid = 0")->select();
        $str = "<span class='g-input-selectbox'><select  onchange=areaChange($(this)) autocomplete='off'><option value= 0>请选择</option>";
        foreach ($areas as $v) {
            $str .= "<option value= " . $v['id'] . ">" . $v['name'] . "</option>";
        }
        $str .= "</select></span>&nbsp;";
        for ($i = $rank - 1; $i > 0; $i--) {
            $str .= "<span class='g-input-selectbox'><select name ='area_" . $i . "' onchange=areaChange($(this)) autocomplete='off'>";

            $str .= "<option value= 0>请选择</option>";
            $str .= "</select></span>&nbsp;";
        }
    }
    echo $str;
}

function getbusiness_timeExplode($id, $ids, $key) {
    $idsArr = explode(",", $ids);
    if ($id == $idsArr[$key]) {
        return "selected";
    }
}

function getTotalNum($table, $day, $mtype) {
    $today = strtotime(date("Y-m-d 00:00:00"));
    $yesterday = $today - 3600 * 24;
    $sevenDay = $today - 3600 * 24 * 6;
    $monthFirst = strtotime(date("Y-m-01 00:00:00"));
    $now = time();
    if ($day == 1) {
        $start = $today;
        $end = time();
    } elseif ($day == -1) {
        $start = $yesterday;
        $end = $today;
    } elseif ($day == 7) {
        $start = $sevenDay;
        $end = $now;
    } elseif ($day == 30) {
        $start = $monthFirst;
        $end = $now;
    } elseif ($day == -30) {
        $last_month = date('m') - 1;
        if (date('m') == 1) {
            $last_month = 12;
        }
        $start = strtotime(date("Y-" . $last_month . "-01 00:00:00"));
        $end = $monthFirst;
    }
    $sql = "1=1";

    if ($start > 0 && $end > 0) {
        $sql .= " AND addtime between " . $start . " AND " . $end . "";
    }
    if ($table == "order_goods") {
        $count = 0;
        $orders = M("order")->where($sql)->select();
        foreach ($orders as $v) {
            $goods = M("order_goods")->where("pid = " . $v['id'] . "")->select();
            foreach ($goods as $v2) {
                if ($mtype == 0) {
                    $count += $v2['gnum'];
                } else {
                    $count += $v2['gnum'] * $v2['gprice'];
                }
            }
        }
    } else {
        $count = M($table)->where($sql)->count();
    }
    return $count;
}

function getShopSaleNum($sid) {
    $shops = M("order")->field("id")->where("sid = " . $sid . "")->select();
    $total = 0;
    foreach ($shops as $v) {
        $goods = M("order_goods")->field("gnum")->where("pid = " . $v['id'] . "")->select();
        foreach ($goods as $v2) {
            $total += $v2['gnum'];
        }
    }
    return $total;
}

function getUserOrdernum($uid) {
    $count = M("order")->where("uid = " . $uid . " AND state = 5")->count();
    return $count;
}

function getUserOrderMoney($uid) {
    $lists = M("order")->field("totalMoney")->where("uid = " . $uid . " AND state = 5")->select();
    $total = 0;
    foreach ($lists as $v) {
        $total +=$v['totalMoney'];
    }
    return $total;
}

function getPageType($id) {
    if ($id > 0) {
        $word = "编辑";
    } else {
        $word = "添加";
    }
    return $word;
}

function getKeyUids($keyword) {
    $lists = M("user")->field("id")->where("name like '%" . $keyword . "%'")->select();
    if ($lists) {
        foreach ($lists as $v) {
            $ids .= $v['id'] . ",";
        }
        $ids = substr($ids, 0, -1);
    }
    return $ids;
}

function isClearTempFile($table) {
    $tables_clear = array("dictionary", "service", "area", "ads", "article_cat", "dish_types", "friends"
        , "goods_attr", "goods_unit", "news", "play", "shop_attr");
    if (in_array($table, $tables_clear)) {
        clearTempFile();
    }
}

function getAreaLists($id, $level, $is_search = 0) {
    $ids = getAreaTopIds($id);
    $num = count($ids);
    $add_id = I('get.add_id', '0', 'int');
    $str = "";
    if ($id > 0) {
        for ($i = 0; $i < $level; $i++) {
            if ($i == 0) {
                $pid = 0;
            } elseif ($i == 1) {
                $pid = $ids[($num - 1)];
            } elseif ($i == 2) {
                $pid = $ids[($num - 2)];
            }
            $areas = M("area")->field("id,name")->where("pid = " . $pid . "")->order("ord ASC,id DESC")->select();
            $str .= "<select  onchange=areaChange($(this)) autocomplete='off'><option value=0>请选择</option>";

            if ($is_search == 1) {
                $is_search_rs = 1111;
            } else {
                $is_search_rs = $num - 1;
            }
            foreach ($areas as $v) {
                if (in_array($v['id'], $ids) && ($i != $is_search_rs or $add_id > 0)) {
                    $select = "selected";
                } else {
                    $select = "";
                }
                $str .= "<option value= " . $v['id'] . " " . $select . ">" . $v['name'] . "</option>";
            }
            $str .= "</select>&nbsp;";
        }
    } else {
        for ($i = 0; $i < $level; $i++) {
            $str .= "<select  onchange=areaChange($(this)) autocomplete='off'><option value=0>请选择</option>";
            if ($i == 0) {
                $areas = M("area")->field("id,name")->where("pid = 0")->order("ord ASC,id DESC")->select();
                foreach ($areas as $v) {
                    if (in_array($v['id'], $ids)) {
                        $select = "selected";
                    } else {
                        $select = "";
                    }
                    $str .= "<option value= " . $v['id'] . " " . $select . ">" . $v['name'] . "</option>";
                }
            }
            $str .= "</select>&nbsp;";
        }
    }
    return $str;
}

function getAreaTopIds($id) {
    if ($id > 0) {
        $arr = array();
        $area = M("area")->field("pid")->where("id = " . $id . "")->find();
        $arr[] = $id; //3
        if ($area['pid'] > 0) {
            $arr[] = $area['pid']; //2
            $area = M("area")->field("pid")->where("id = " . $area['pid'] . "")->find();
            if ($area['pid'] > 0) {
                $arr[] = $area['pid']; //1
            }
        }
        return $arr;
    }
}

function getHighchartsData($mtype, $t_start, $t_end, $shop_id) {
    $sql = "addtime between " . $t_start . " AND " . $t_end . "  AND state =5";
    if ($shop_id > 0) {
        $sql .= " AND sid = " . $shop_id . "";
    }
    if ($mtype == 'order') {
        $num = M("order")->where($sql)->count();
        $rs = intval($num);
    } else if ($mtype == 'users') {
        $sql = "addtime between " . $t_start . " AND " . $t_end . "";
        $num = M("user")->where($sql)->count();
        $rs = intval($num);
    } else {
        $money = M("order")->where($sql)->sum('realMoney');
        $rs = intval($money);
    }
    return $rs;
}

function getHighchartsLists($mtype, $t_start, $t_end, $shop_id) {
    if ($mtype == 'users') {
        $sql = "addtime between " . $t_start . " AND " . $t_end . "";
        $num = M("user")->where($sql)->count();
        return array("num" => intval($num));
    } else {
        $sql = "addtime between " . $t_start . " AND " . $t_end . "  AND state =5";
        if ($shop_id > 0) {
            $sql .= " AND sid = " . $shop_id . "";
        }
        $lists = M("order")->field('realMoney,pay_method')->where($sql)->select();
        $total = 0;
        $delivery = 0;
        $online = 0;
        $other = 0;
        if ($mtype == 'turnover') {
            foreach ($lists as $v) {
                $total +=$v['realMoney'];
                if ($v['pay_method'] == 0) {
                    $delivery += $v['realMoney']; //货到付款
                } else if ($v['pay_method'] == 8) {
                    $other += $v['realMoney']; //余额支付
                } else if (in_array($v['pay_method'], array("1", "2", "3"))) {
                    $online += $v['realMoney']; //在线支付
                }
            }
        } else {
            foreach ($lists as $v) {
                $total +=1;
                if ($v['pay_method'] == 0) {
                    $delivery += 1; //货到付款
                } else if ($v['pay_method'] == 8) {
                    $other += 1; //余额支付
                } else if (in_array($v['pay_method'], array("1", "2", "3"))) {
                    $online += 1; //在线支付
                }
            }
        }
        return array("total" => $total, "num" => count($lists), "delivery" => $delivery, "online" => $online, "other" => $other);
    }
}

function getMonthLastDay($month, $year) {
    switch ($month) {
        case 4 :
        case 6 :
        case 9 :
        case 11 :
            $days = 30;
            break;
        case 2 :
            if ($year % 4 == 0) {
                if ($year % 100 == 0) {
                    $days = $year % 400 == 0 ? 29 : 28;
                } else {
                    $days = 29;
                }
            } else {
                $days = 28;
            }
            break;

        default :
            $days = 31;
            break;
    }
    return $days;
}
function last_month_today($time) {
     $last_month_time = mktime(date("G", $time), date("i", $time),
                date("s", $time), date("n", $time), - 1, date("Y", $time));
     return date(date("Y-m", $last_month_time) . "-d H:i:s", $time);
}


?>